#!/bin/bash
##################################################
# 脚本说明: 服务启停脚本
# 前提条件: 使用hadoop用户启动
# 启动方法：sh smMain.sh
# 参数说明: source ${PROGDIR}/conf/init.cfg
#-------------------------------------------------
# 创建: tangxin  2021/6/10
# 修改:
# 修改时间   修改人   修改说明
#
#
##################################################

## 获取上下文路径、本脚本名
readonly PROGDIR=$(readlink -m $(dirname $0))
readonly PROGNAME=$(basename $0)
readonly HOSTNAME=`hostname`

PID_DIR=$PROGDIR/pid
mkdir -p $PID_DIR

## 打印输出函数，输出内容格式化；调用方法：format_print 级别 "输出内容"
function format_print(){
  LEVEL=$1
  INFO=$2
  if [[ $LEVEL == 'INFO' ]];then
    echo -e "`date "+%Y-%m-%d %H:%M:%S"` [INFO] ${INFO}" 
  elif [[ $LEVEL == 'FAILED' ]];then
    echo -e "`date "+%Y-%m-%d %H:%M:%S"` \e[31m[FAILED] ${INFO}\e[0m" 
  elif [[ $LEVEL == 'SUCCESS' ]];then
    echo -e "`date "+%Y-%m-%d %H:%M:%S"` \e[32m[SUCCESSFUL] ${INFO}\e[0m" 
  fi
}

## 必须使用hadoop用户执行
if [ $USER != "hadoop" ]; then
  format_print FAILED "Must run as hadoop"
  exit 1
fi

## 读取配置文件
source $PROGDIR/conf/init.cfg

function STARTZOOKEEPER(){
  if [[ ! -f $PID_DIR/zookeeper.pid ]];then
    format_print INFO "启动 Zookeeper "
    for HOST in ${ZOOKEEPER_HOSTS[@]}
    do
      format_print INFO "$HOST"
      ssh $HOST "source $HADOOP_DIR/.bash_profile ; cd $HADOOP_DIR/$ZOOKEEPER_VERSION; zkServer.sh start"
    done
    echo `ps -ef | grep 'QuorumPeerMain' | grep -v 'grep' | awk '{print $2}'` > $PID_DIR/zookeeper.pid
    for i in ${ZOOKEEPER_HOSTS[@]}
    do
      format_print INFO "$HOST"
      ssh $HOST "source $HADOOP_DIR/.bash_profile ; cd $HADOOP_DIR/$ZOOKEEPER_VERSION; zkServer.sh status"
    done
  else
    format_print FAILED "PID Exists : $PID_DIR/zookeeper.pid "
  fi
}

function STOPZOOKEEPER(){
  if [[ -f $PID_DIR/zookeeper.pid ]];then
    format_print INFO "停止 Zookeeper "
    for HOST in ${ZOOKEEPER_HOSTS[@]}
    do
      format_print INFO "$HOST"
      ssh $HOST "$HADOOP_DIR/$ZOOKEEPER_VERSION/bin/zkServer.sh stop "
    done
    rm -f $PID_DIR/zookeeper.pid
  else
    format_print INFO "PID Not Exists : $PID_DIR/zookeeper.pid "
  fi
}

function STARTHADOOP(){
  if [[ ! -f $PID_DIR/hadoop.pid ]];then
    format_print INFO "启动 Hadoop "
    ssh $HDFS_MASTER "source $HADOOP_DIR/.bash_profile;start-all.sh"
    echo `ps -ef | grep '\.NameNode' | grep -v 'grep' | awk '{print $2}'` > $PID_DIR/hadoop.pid
  else
    format_print FAILED "PID Exists : $PID_DIR/hadoop.pid "
  fi
}

function STOPHADOOP(){
  if [[ -f $PID_DIR/hadoop.pid ]];then
    format_print INFO "停止 Hadoop "
    ssh $HDFS_MASTER "source $HADOOP_DIR/.bash_profile;stop-all.sh"
    rm -f $PID_DIR/hadoop.pid
  else
    format_print FAILED "PID Not Exists : $PID_DIR/hadoop.pid "
  fi
}

function STARTHIVE(){
  if [[ ! -f $PID_DIR/hivemetastore.pid ]];then
    format_print INFO "启动 Hive "
    ssh $HDFS_MASTER "source $HADOOP_DIR/.bash_profile ; nohup hive --service metastore > /dev/null 2>&1 & "
    echo `ps -ef | grep 'HiveMetaStore' | grep -v 'grep' | awk '{print $2}'` > $PID_DIR/hivemetastore.pid
    for HOST in ${HIVE_HOSTS[@]}
    do
      format_print INFO "$HOST"
      ssh $HOST "source $HADOOP_DIR/.bash_profile ; nohup hive --service hiveserver2 > /dev/null 2>&1 &"
    done
  else
    format_print FAILED "PID Exists : $PID_DIR/hivemetastore.pid "
  fi
}

function STOPHIVE(){
  if [[ -f $PID_DIR/hivemetastore.pid ]];then
    format_print INFO "停止 Hive "
    
    ## 停止 HiveMetaStore
    PID=$(ssh ${HDFS_MASTER} "ps -ef | grep 'HiveMetaStore' | grep -v grep" | awk '{print $2}')
    if [ -n "$PID" ]; then
      echo "${HDFS_MASTER}: Stopping HiveMetaStore ,pid : ${PID}"
      ssh ${HDFS_MASTER} "kill -9 ${PID}"
    else
      echo "${HDFS_MASTER}: HiveMetaStore is not runing"
    fi
    if [[ $? == 0 ]];then
      rm -f $PID_DIR/hivemetastore.pid
    fi
    
    ## 停止 HiveServer2
    for HOST in ${HIVE_HOSTS[@]}
    do
      PID=$(ssh ${HOST} "ps -ef | grep 'HiveServer2' | grep -v grep" | awk '{print $2}')
      if [ -n "$PID" ]; then
        echo "${HOST}: Stopping HiveServer2 ,pid : ${PID}"
        ssh ${HOST} "kill -9 ${PID}"
      else
        echo "${HOST}: HiveServer2 is not runing"
      fi
    done
  else
    format_print FAILED "PID Not Exists : $PID_DIR/hivemetastore.pid"
  fi
}

function STARTHBASE(){
  if [[ ! -f $PID_DIR/hbase.pid ]];then
    format_print INFO "启动 HBASE "
    start-hbase.sh
    echo `ps -ef | grep '\.NameNode' | grep -v 'grep' | awk '{print $2}'` > $PID_DIR/hbase.pid
  else
    format_print FAILED "PID Exists : $PID_DIR/hbase.pid "
  fi
}

function STOPHBASE(){
  if [[ -f $PID_DIR/hbase.pid ]];then
    format_print INFO "停止 HBASE "
    hbase-daemon.sh stop master
    hbase-daemons.sh stop regionserver
    rm -f $PID_DIR/hbase.pid
  else
    format_print FAILED "PID Not Exists : $PID_DIR/hbase.pid "
  fi
}

function STARTALL(){
  format_print INFO "启动所有服务"
  STARTZOOKEEPER
  STARTHADOOP
  STARTHIVE
  STARTHBASE
}

function STOPALL(){
  format_print INFO "停止所有服务"
  STOPHBASE
  STOPHIVE
  STOPHADOOP
  STOPZOOKEEPER
  
}

function CHECKSTATE(){
  format_print INFO "检查进程 "
  for i in `grep -v '127\|::1' /etc/hosts | awk '{print $1}'`
  do 
    echo -e "\n$i" 
    ssh $i "source $HADOOP_DIR/.bash_profile ; jps | grep -v 'Jps'"
  done
}

###################################################################################################
function MAIN(){
  CHECKSTATE
  if read -t 10 -p "  欢迎使用 一键启停脚本
    1. 启动 Zookeeper
    2. 停止 Zookeeper
    3. 启动 Hadoop
    4. 停止 Hadoop
    5. 启动 Hive
    6. 停止 Hive
    7. 启动 HBase
    8. 停止 HBase
    99. 启动所有服务
    00. 停止所有服务
  请输入服务编号并回车，退出请按Ctrl+C ：" NUM ;then
    case $NUM in
      1) STARTZOOKEEPER
         MAIN
      ;;
      2) STOPZOOKEEPER
         MAIN
      ;;
      3) STARTHADOOP
         MAIN
      ;;
      4) STOPHADOOP
         MAIN
      ;;
      5) STARTHIVE
         MAIN
      ;;
      6) STOPHIVE
         MAIN
      ;;
      7) STARTHBASE
         MAIN
      ;;
      8) STOPHBASE
         MAIN
      ;;
     99) STARTALL
         MAIN
      ;;
     00) STOPALL
         MAIN
      ;;
      *) format_print INFO "没有该选项，请重新输入!!! \n\n"
         MAIN
      ;;
    esac
  else 
    echo -e "\n  10s内未输入任何值,超时退出"
    exit 0
  fi
}

MAIN
sleep 3
CHECKSTATE

